package com.wish.dynamicproxy;

import com.wish.inter.IPerson;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author: suliucheng
 * @Date: 2021/2/24 21:38
 * @Description:
 */
public class JDKMeipo implements InvocationHandler {

    private IPerson person;

    public Object getProxyInstance(IPerson person){
        this.person=person;
        Class<? extends IPerson> aClass = person.getClass();
        return  Proxy.newProxyInstance(aClass.getClassLoader(), aClass.getInterfaces(),this);
    }



    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(this.person, args);
        after();
        return invoke;
    }

    private void after() {
        System.out.println("牵手成功，结婚！");
    }

    private void before() {
        System.out.println("找到媒婆，媒婆代理开始找对象。。。");
    }
}
